package cn.com.zpark.dao.impl;

import cn.com.zpark.dao.ILoginDao;
import cn.com.zpark.pojo.User;
import cn.com.zpark.utils.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @PackageName: cn.com.zpark.dao.impl
 * @ClassName: LoginDaoImpl
 * @Description:用户登录持久层实现类
 * @author: RZS
 * @date: 2021/12/12  9:06
 */
public class LoginDaoImpl implements ILoginDao {

    private Connection conn = null;
    private PreparedStatement stat = null;
    private ResultSet res = null;

    /**
     * 根据用户名查询用户数据
     * @param username 用户名
     * @return user对象
     */
    @Override
    public User selectByUsername(String username) {
        User user = null;
        try {
            // 1、获取连接
            conn = DruidUtils.getConn();
            // 2、创建sql
            String sql = "select * from user where uname=?";
            // 3、获取预编译sql对象
            stat = conn.prepareStatement(sql);
            // 4、替换占位符
            stat.setString(1, username);
            // 5、执行sql
            res = stat.executeQuery();
            // 6、封装数据
            while (res.next()) {
                user = new User();
                user.setId(res.getInt("id"));
                user.setUname(res.getString("uname"));
                user.setPwd(res.getString("pwd"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // 7、关闭资源
            DruidUtils.close(res, stat, conn);
        }

        // 返回数据
        return user;
    }
}
